package com.abing.starry.controller;

import com.abing.starry.common.ErrorCode;
import com.abing.starry.exception.ThrowUtils;
import com.abing.starry.model.entity.TodoEntity;
import com.abing.starry.model.enums.todo.TodoStatusEnum;
import com.abing.starry.model.vo.todo.TodoVO;
import com.abing.starry.service.TodoService;
import com.abing.starry.util.TokenUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @Author CaptainBing
 * @Date 2024/8/11 20:24
 * @Description
 */
@RestController
@RequestMapping("/todo")
@RequiredArgsConstructor
public class TodoController {

    private final TodoService todoService;

    @PostMapping("/add")
    @Operation(summary = "添加待办事项")
    public boolean addTodo(@RequestBody TodoEntity todoEntity){

        ThrowUtils.throwIf(StringUtils.isEmpty(todoEntity.getTitle()), ErrorCode.PARAMS_ERROR,"标题不能为空");

        String userId = TokenUtils.getCurrentUser().getId();
        todoEntity.setUserId(userId);

        return todoService.save(todoEntity);
    }

    @GetMapping("/list")
    @Operation(summary = "根据用户id获取待办事项列表")
    public Map<String, List<TodoVO>> listTodoByUserId(){

        String userId = TokenUtils.getCurrentUser().getId();
        List<TodoVO> todoEntities = todoService.list(
                Wrappers.lambdaQuery(TodoEntity.class)
                        .eq(TodoEntity::getUserId, userId))
                .stream()
                .map(todoEntity -> {
                    TodoVO todoVO = new TodoVO();
                    BeanUtils.copyProperties(todoEntity, todoVO);
                    if (todoEntity.getStatus().equals(TodoStatusEnum.unfinished.getValue())) {
                        todoVO.setStatus(TodoStatusEnum.unfinished.name());
                        return todoVO;
                    }
                    todoVO.setStatus(TodoStatusEnum.finished.name());
                    return todoVO;
                }).collect(Collectors.toList());


        Map<String, List<TodoVO>> todoMap = todoEntities
                .stream()
                .sorted(Comparator.comparing(TodoVO::getUpdateTime).reversed())
                .collect(Collectors.groupingBy(TodoVO::getStatus));

        return todoMap;
    }

    @GetMapping("/del/{id}")
    @Operation(summary = "根据id删除待办事项")
    public boolean deleteTodo(@PathVariable String id){
        return todoService.removeById(id);
    }

    @GetMapping("/date/get")
    @Operation(summary = "根据日期获取待办事项")
    public List<TodoEntity> getTodoByCurrentDate(@RequestParam Long createTime){

        String userId = TokenUtils.getCurrentUser().getId();
        long oneDay = 60 * 60 * 24 * 1000L;
        List<TodoEntity> todoEntities = todoService.list(
                Wrappers.lambdaQuery(TodoEntity.class)
                        .eq(TodoEntity::getUserId, userId)
                        .between(TodoEntity::getCreateTime, createTime, createTime + oneDay));

        return todoEntities;
    }


}
